package com.cms.recursoshumanos.activity.pantallas_principal;

import com.cms.recursoshumanos.activity.beneficios_lot.ListaBeneficiosAdmActivity;
import com.cms.recursoshumanos.activity.directorio.DirectorioMainActivity;
import com.cms.recursoshumanos.activity.eventos.ListaTipoEventoActivity;
import com.cms.recursoshumanos.activity.main.MainRecursosHumanosActivity;
import com.cms.recursoshumanos.activity.user.UserInfoActicity;
import com.cms.recursoshumanos.activity.vacaciones.DetailVacacionesActivity;
import com.cms.recursoshumanos.cache.ListaContactosCache;
import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.handler.InfoUserActivityHandler;
import com.cms.recursoshumanos.handler.ListaContactoScreenHandler;
import com.cms.recursoshumanos.handler.ListaTipoEventosActivityHandler;
import com.cms.recursoshumanos.object.Usuario;
import com.cms.recursoshumanos.utilities.Tool;
import com.example.frontendrecursoshumanos.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;

public class PrincipalMainMenuActivity extends MainRecursosHumanosActivity 
{

	protected static InfoUserAsync infoUserAsyncTask;
	protected static TipoEventoAsync tipoEventoAsyncTask;
	protected static ListaContactoAsync listaContactoAsyncTask;
	
	private ProgressDialog dialog;
	
	private static Usuario elUsuario;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu_principal);
		elUsuario = UserInfoCache.getInstance().getUsuario();
	}
	@Override
	protected void onResume() 
	{
		super.onResume();
		if (elUsuario == null)
		{
			elUsuario = UserInfoCache.getInstance().getUsuario();
		}
		Tool.getInstance().getListaContactoShared();
	}


	public void showDirectorio(View v)
    {
		findDirectorio();
	}
	public void showVacaciones(View v)
    {
		goVacaciones();
	}
	public void showInfoPersona(View v)
    {
		findInfoUser();
	}
	public void showTipoEvento(View v)
    {
		findInfoTipoEvento();
	}
	public void showBeneficio(View v)
    {
		goBeneficioPrincipal();
	}
	public void showNomina(View v)
    {
		goNomina();
	}
	
	protected void findDirectorio()
	{
		listaContactoAsyncTask = new ListaContactoAsync(elUsuario);
		listaContactoAsyncTask.execute();			
	}
	
	protected void findInfoUser()
	{
		infoUserAsyncTask = new InfoUserAsync(elUsuario);
		infoUserAsyncTask.execute();			
	}

	protected void findInfoTipoEvento()
	{
		tipoEventoAsyncTask = new TipoEventoAsync(elUsuario);
		tipoEventoAsyncTask.execute();	
	}
	public void goBeneficioPrincipal()
	{
		Intent intent = new Intent(PrincipalMainMenuActivity.this, PrincipalBeneficiosMenuActivity.class);
		startActivity(intent);
	}
	public void goInfoUser()
	{
		Intent intent = new Intent(PrincipalMainMenuActivity.this, UserInfoActicity.class);
		startActivity(intent);
	}
	public void goNomina()
	{
		Intent intent = new Intent(PrincipalMainMenuActivity.this, PrincipalNominaMenuActivity.class);
		startActivity(intent);
	}
	public void goTipoEvento()
	{
		Intent intent = new Intent(PrincipalMainMenuActivity.this, ListaTipoEventoActivity.class);
		startActivity(intent);
	}

	public void goBeneficioAdm()
	{
		Intent intent = new Intent(PrincipalMainMenuActivity.this, ListaBeneficiosAdmActivity.class);
		startActivity(intent);
	}
	public void goVacaciones()
	{
		Intent intent = new Intent(PrincipalMainMenuActivity.this, DetailVacacionesActivity.class);
		startActivity(intent);
	}
	public void goDirectorio()
	{
		Intent intent = new Intent(PrincipalMainMenuActivity.this, DirectorioMainActivity.class);
		startActivity(intent);
	}
	
	
	private class InfoUserAsync extends AsyncTask<String, Void, String>
	{
		private Usuario elUsuario;
		
		public InfoUserAsync(Usuario elUsuario)
		{
			this.elUsuario = elUsuario;

		}
		@SuppressWarnings("deprecation")
		@Override
		protected void onPreExecute()
		{
			showDialog(0);
			dialog = ProgressDialog.show(PrincipalMainMenuActivity.this, "Espere","Cargando...",true );
		}
	
		@Override
		protected String doInBackground(String... arg0) 
		{
			InfoUserActivityHandler handler = new InfoUserActivityHandler();

			String result = handler.GetUserInfo(this.elUsuario.getNombre_Usuario(),	
													this.elUsuario.getPassword());
			return result;
		}
		
		@Override
		public void onPostExecute(String  result)
		{
			
			
			String str_result = result;
			
			if (str_result.equals(Constant.CODE_OK))
			{ 
				Tool.getInstance().saveUserInfoShared(1,UserInfoCache.getInstance().getEmpleado());
				goInfoUser();
			}
			else 
			{
				Tool.getInstance().showMessage( str_result);
			}
			dialog.cancel();
			this.cancel(true);
		}
	}
	private class TipoEventoAsync extends AsyncTask<String, Void, String>
	{
		private Usuario elUsuario;
		
		public TipoEventoAsync(Usuario elUsuario)
		{
			this.elUsuario = elUsuario;
		}
		
		@SuppressWarnings("deprecation")
		@Override
		protected void onPreExecute()
		{
			showDialog(0);
			dialog = ProgressDialog.show(PrincipalMainMenuActivity.this, "Espere","Cargando...",true );
		}
	
		@Override
		protected String doInBackground(String... arg0) 
		{
			ListaTipoEventosActivityHandler handler = new ListaTipoEventosActivityHandler();
			String result = handler.getListaTipoEvento(this.elUsuario.getNombre_Usuario(),
					this.elUsuario.getPassword());
			return result;
		}
		
		@Override
		public void onPostExecute(String  result)
		{
		
			String str_result = result;
			
			if (str_result.equals(Constant.CODE_OK))
			{ 
				goTipoEvento();
			}
			else 
			{
				Tool.getInstance().showMessage( str_result);
			}
			dialog.cancel();
			this.cancel(true);
		}	
	}
	
	public class ListaContactoAsync extends AsyncTask<Void, Void, String>
	{
		private Usuario elUsuario;
		
		public  ListaContactoAsync(Usuario elUsuario)
	    {
		    this.elUsuario = elUsuario;
	    }
		@SuppressWarnings("deprecation")
		@Override
		protected void onPreExecute()
		{
			showDialog(0);
			dialog = ProgressDialog.show(PrincipalMainMenuActivity.this, "Espere","Cargando...",true );
		}
		@Override
		protected String doInBackground(Void... arg0) 
		{
			ListaContactoScreenHandler handler = new ListaContactoScreenHandler();
			String result = handler.getListaContacto(this.elUsuario.getNombre_Usuario(),
														this.elUsuario.getPassword());
			return result;
		}
		
		@Override
		public void onPostExecute(String  result)
		{
			

			Tool.getInstance().saveContactInfoShared(ListaContactosCache.getInstance());
			goDirectorio();
			dialog.cancel();
			this.cancel(true);
			
			
		}
		
	}
	
}
